package com.example.lib_adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import androidx.recyclerview.widget.RecyclerView

abstract class BaseQuickMultipleAdapter<T:MutipleEntity, U>:RecyclerView.Adapter<BaseViewHolder> (){

    var data=ArrayList<T>()

    @JvmField
    var onItemClickListener:OnItemClickListener?=null
    @JvmField
    var onItemLongClickListener:OnItemLongClickListener?=null

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val layoutManager = recyclerView.layoutManager
        if(layoutManager is GridLayoutManager){
            val manager = layoutManager as GridLayoutManager
            manager.spanSizeLookup=object :SpanSizeLookup(){
                override fun getSpanSize(position: Int): Int {
                    return getMutipleSpanSize(position,recyclerView)
                }

            }
        }
    }

    //添加数据
    fun addData(entity: T){
        data.add(entity)
        notifyItemInserted(data.size-1)
    }

    fun addAll(list: List<T>){
        data.clear()
        data.addAll(list)
        notifyDataSetChanged()
    }

    fun getData():List<T>{
        return data
    }

    fun removeData(position: Int){
        data.removeAt(position)
        notifyItemRemoved(position)
    }


    abstract fun getMutipleSpanSize(position: Int, recyclerView: RecyclerView): Int

    //创建ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        var view=createItemView(viewType,parent)
        val viewHolder = BaseViewHolder(view)
        if(onItemClickListener!=null){
            viewHolder.itemView.setOnClickListener {
                onItemClickListener!!.onItemClick(viewHolder.adapterPosition)
            }
        }
        if(onItemLongClickListener!=null){
            viewHolder.itemView.setOnLongClickListener {
                onItemLongClickListener!!.onItemLongClick(viewHolder.adapterPosition)
            }
        }
        return viewHolder
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener){
        this.onItemClickListener=onItemClickListener
    }

    fun setOnItemLongClickListener(onItemLongClickListener: OnItemLongClickListener){
        this.onItemLongClickListener=onItemLongClickListener
    }


    abstract fun createItemView(viewType: Int, parent: ViewGroup): View

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        val t = data.get(position) as T
        convert(holder,t)
    }

    abstract fun convert(holder: BaseViewHolder, t: MutipleEntity)

    abstract override fun getItemViewType(position: Int): Int

    override fun getItemCount(): Int {
        return data.size
    }

    interface OnItemClickListener{
        fun onItemClick(position: Int)
    }

    interface OnItemLongClickListener{
        fun onItemLongClick(position: Int):Boolean
    }
}